<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Elements
{
    protected $obj;
    protected $sesja;
    
    public function __construct()
    {
        $this->obj =& get_instance();
    }
    
    public function load_view($sesja,$view='',$cont='',$head='',$foot='')
    {
        $nfoot['copy']  = 'United Team &copy;'.date('Y').' wszelkie prawa zastrzeżone.';
        $nfoot['autor'] = 'Piotr Nalepa';
        $nhead['title'] = (empty($head['stitle'])) ? $this->obj->config->item('nazwa_strony') : $head['stitle'].' | '.$this->obj->config->item('nazwa_strony');
        $nhead['desc']  = (empty($head['sdesc'])) ? $this->obj->config->item('opis_strony') : $head['sdesc'];
        $nhead['keys']  = (empty($head['skeys'])) ? $this->obj->config->item('keys_strony') : $head['skeys'];
        $nhead['menu']  = $this->show_menu($sesja);
        $nhead['styl']  = $this->obj->config->item('css');
        $nhead['url']   = $this->obj->config->base_url();
        if(empty($view))
            $view = 'default/start';
        $head = array_merge($nhead,(array)$head);
        $foot = array_merge($nfoot,(array)$foot);
        $this->obj->load->view('default/header',$head);
        $this->obj->load->view($view,$cont);
        $this->obj->load->view('default/footer',$foot);
    }
    
    public function load_alert($sesja,$class,$alert,$link='')
    {
        $tab['alert']   = $alert;
        $tab['class']   = $class;
        $tab['link']    = $link;
        $foot['copy']   = 'United Team &copy;'.date('Y').' wszelkie prawa zastrzeżone.';
        $foot['autor']  = 'Piotr Nalepa';
        switch($class)
        {
            case 'error':
                $head['h1'] = 'Wystąpił błąd!';
                break;
            case 'success':
                $head['h1'] = 'Operacja zakończona sukcesem!';
                break;
            case 'alert':
                $head['h1'] = '';
                break;
            default:
                $head['h1'] = '';
                break;
        }
        $head['title'] = $this->obj->config->item('nazwa_strony');
        $head['menu']  = $this->show_menu($sesja);
        $head['styl']  = $this->obj->config->item('css');
        $head['url']   = $this->obj->config->base_url();
        $this->obj->load->view('default/header',$head);
        $this->obj->load->view('default/alert',$tab);
        $this->obj->load->view('default/footer',$foot);
    }
    
    public function show_menu($sesja='',$class='')
    {
        $menu    = '<ul class="nav '.$class.'">';
        if(isset($sesja['id']))
            $menu .= '<li>'.anchor('users/show_panel','Twój profil').'</li>';
        $menu .= '<li>'.anchor('league/show_league/'.$this->obj->config->item('ut_liga').'/tabela_united_team','Tabela ligowa United Team').'</li>';
        /*
        * admin    = 1
        * edytor   = 1
        * piłkarz  = 1
        * trener   = 11
        * 
        * dodajemy wartości: typ, admin, editor z sesji aby otrzymać numer uprawnienia do linków
        */
        if(isset($sesja['typ']))
            $typ = (int)$sesja['typ']+(int)$sesja['editor']+(int)$sesja['admin'];
        else
            $typ = 0;
        $wynik = $this->obj->db->query('SELECT link_id, link_title, link_href, link_anchor, link_rights, link_class '
                .'FROM ut_links '
                .'WHERE link_active=1 AND link_parent IS NULL AND link_divider=0 '
                .'ORDER BY link_order');
        if($wynik->num_rows()>0)
        {
            foreach($wynik->result_array() as $w)
            {
                if($this->has_sublevels($w['link_id']))
                {
                    $opt     = array(
                        'class'         => $w['link_class'],
                        'title'         => $w['link_title'],
                        'data-toggle'   => 'dropdown' 
                    );
                    $menu   .= '<li class="dropdown">'.anchor($w['link_href'],$w['link_anchor'].' <b class="caret"></b>',$opt);
                    $menu   .= '<ul class="dropdown-menu">';
                    $subs   = $this->get_sublevels($w['link_id']);
                    foreach($subs as $s)
                    {
                        $tab = json_decode($s['rights']);
                        if(in_array($typ,(array)$tab) || $tab[0]=='0')
                        {
                            if($s['divider']==1)
                                $menu   .= '<li class="divider"></li>';
                            else
                            {
                                if(!empty($s['icon']))
                                    $menu .= '<li>'.anchor($s['href'],'<i class="'.$s['icon'].'"></i> '.$s['anchor'],'title="'.$s['title'].'"').'</li>';
                                else
                                    $menu .= '<li>'.anchor($s['href'],$s['anchor'],'title="'.$s['title'].'"').'</li>';
                            }
                        }
                    }
                    $menu   .= '</ul>';
                    $menu   .= '</li>';
                }
                else
                {
                    $opt     = array(
                        'class' => $w['link_class'],
                        'title' => $w['link_title']
                    );
                    $menu   .= '<li>'.anchor($w['link_href'],$w['link_anchor'],$opt).'</li>';
                }

            }
            if(isset($sesja['admin']) && $sesja['admin']==1)
                $menu .= '<li>'.anchor('site/options','Opcje systemu').'</li>';
            if(isset($sesja['id']))
                $menu .= '<li>'.anchor('site/logout_user','Wyloguj się').'</li>';
            else
                $menu .= '<li>'.anchor('site/login','Zaloguj się').'</li>';
        }
        $menu .= '</ul>';
        return $menu;
    }
    
    protected function get_sublevels($id)
    {
        $tab    = '';
        $kids   = $this->obj->db
                ->select('link_id, link_title, link_anchor, link_href, link_icon, link_rights, link_divider')
                ->order_by('link_order')
                ->get_where('ut_links',array('link_active'=>1,'link_parent'=>$id));
        if($kids->num_rows()>0)
            foreach($kids->result_array() as $k)
            {
                $tab[$k['link_id']]['title']  = $k['link_title'];
                $tab[$k['link_id']]['anchor'] = $k['link_anchor'];
                $tab[$k['link_id']]['href']   = $k['link_href'];
                $tab[$k['link_id']]['icon']   = $k['link_icon'];
                $tab[$k['link_id']]['rights'] = $k['link_rights'];
                $tab[$k['link_id']]['divider']= $k['link_divider'];
            }
        return $tab;
    }
    
    protected function has_sublevels($id)
    {
        $wynik = $this->obj->db->get_where('ut_links',array('link_active'=>1,'link_parent'=>$id));
        if($wynik->num_rows()>0)
            return TRUE;
        else
            return FALSE;
    }
   
}

/* End of file site_elements.php */
/* Location: ./application/libraries/site_elements.php */